﻿using System;
using AppsGenerator.Models;
using AppsGenerator.Generators;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace AppsGenerator.WebGenerators.WebViewGenerator
{
    public abstract class BaseEditViewItemAlgorithm : BaseGenerator, IEditViewItemAlgorithm
    {
        #region Fields

        public static class AbstractBaseConsts
        {
            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstTableHeaders = "[TableHeaders]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstTableFields = "[TableFields]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstTableEditFields = "[TableEditFields]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstReferToEditAction = "[ReferToEditAction]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstReferToDeleteAction = "[ReferToDeleteAction]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstReferToSelectAction = "[ReferToSelectAction]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstPrimaryKeyType = "[PrimaryKeyType]";
        }

        /// <summary>
        /// The instance of ViewComponents, this object will be updated when deserialization pattern file
        /// </summary>
        public ViewComponents ViewComponents { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        protected BaseEditViewItemAlgorithm()
        {
            this.DirPath = Path.Combine(this.GetAppPath(), typeof(WebGenerator).Name, "Views", "Admin", "Partials");
        }
        
        #endregion

        #region Methods

        /// <summary>
        /// Method for generating contents of partial list view components and save generated contents to a file
        /// </summary>
        /// <param name="tbl">The data source is corresponding with class which will be generated</param>
        public abstract void GenerateContents(DBBuilder.DBDataStructure.Table tbl);

        /// <summary>
        /// Deserialize Base Controller pattern file
        /// </summary>
        public void DeserializeTemplate()
        {
            var patternFile = Path.Combine(this.GetAppPath(), "Templates", "WebTemplates", this.XMLTemplate);

            using (XmlTextReader xreader = new XmlTextReader(patternFile))
            {
                xreader.Namespaces = false;
                try
                {
                    var x = new XmlSerializer(typeof(ViewComponents));
                    this.ViewComponents = x.Deserialize(xreader) as ViewComponents;
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Cannot deserialize BaseEditViewItemAlgorithm, BaseEditViewItemAlgorithm => {0}", ex.Message));
                }
            }
        }

        #endregion
    }
}
